package com.tamartech.greendb.util;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.tamartech.greendb.dao.AddressDAO;
import com.tamartech.greendb.dao.ElectoralRollDAO;
import com.tamartech.greendb.dao.ElectoralRollEntryDAO;
import com.tamartech.greendb.dao.WardDAO;
import com.tamartech.greendb.data.Address;
import com.tamartech.greendb.data.ElectoralRoll;
import com.tamartech.greendb.data.ElectoralRollEntry;
import com.tamartech.greendb.data.Ward;


public class StreetLister
{

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception
    {
        String wardName = args[0];
        String pollingDistrict = args[1]; // KA, KB etc

        WardDAO wDAO = new WardDAO();
        Ward w = wDAO.get(wardName);
        
        if (w == null)
            throw new Exception ("Ward " + wardName + " not found");
        
        ElectoralRollDAO erDAO = new ElectoralRollDAO();        
        ElectoralRoll roll = erDAO.getLatestByWard(w);
        
        if (roll == null)
            throw new Exception ("Latest electoral roll for ward: " + wardName + " not found");
        
        ElectoralRollEntryDAO ereDAO = new ElectoralRollEntryDAO();        
        List<ElectoralRollEntry> entries = ereDAO.listByPollingDistrict(roll, pollingDistrict);
        
        AddressDAO aDAO = new AddressDAO();
        
        Set<String> streets = new TreeSet<String>();
        
        for (ElectoralRollEntry e : entries)
        {
            Address a = e.getAddress();
            
            streets.add(filterString (a.getAddress1()));
            streets.add(filterString (a.getAddress2()));
//            System.out.println (a.getAddress1() + "," + a.getAddress2());
        }
        
        for (String street : streets)
            System.out.println (street);
            
    }
    
    private static String filterString (String in)
    {
        StringBuffer sb = new StringBuffer();
 
        for (int i = 0; i < in.length(); i++)
        {
            char ch = in.charAt(i);
            
            if (!Character.isDigit(ch))
                sb.append(ch);
        }
        
        return sb.toString().trim().toUpperCase();
    }

}
